package com.sp.study.model.entity;

import com.sun.source.tree.LambdaExpressionTree;

import java.util.ArrayList;

/**
 * <h1>
 * 实现Aggregate接口
 * </h1>
 *
 * @author Sp
 * @date 2020/9/16 - 19:39
 * @since 2020/9/16 - 19:39 星期三 by Sp Sp287940@163.com
 */
public class BookShelf implements Aggregate{

    /**
     * Book类型动态数组
     */
    private ArrayList<Book> books;
    /**
     * 最后一本书的序号
     */
    private int last = 0;

    public BookShelf(int maxSize){  //生成最大为maxSize大小的Book
        this.books = new ArrayList<Book>(maxSize);
    }

    public Book getBookAt(int indxe){   //获取序号为index的书
        return books.get(indxe);
    }

    public void appendBook(Book book){
        this.books.add(last,book);
        //添加book，序号自增
        last++;
        //this.books.set(last++, book);
    }

    /**
     * 返回书架书本数目
     * @return int
     */
    public int getLength(){     //返回书架book的总数目
        return last;
    }

    @Override
    public Iterator iterator() {
        return new BookShelfIterator(this);
    }
}
